﻿using System;
using System.Collections.Generic;
using System.Linq;
using ZX.TR.Bussiness;
using ZX.TR.Library;
using ZX.EHR.Library;
using ZX.Web.UI.Controls;
using ZX.Common;
using ZX.BLL;
using ZX.DBModule;
using System.Text.RegularExpressions;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;

namespace ZX.WebApp.TR
{
    public partial class QuestionnaireSend : BasePage
    {
        BTrQuestionnaireSubject ts = new BTrQuestionnaireSubject();
        IList<DatUser> strList;
        protected void Page_Load(object sender, EventArgs e)
        {
            string strJson = this.GetStringFromQueryString("testjson", "");
            if (strJson == "" || strJson == null)
            {
                if (!this.IsPostBack)
                {
                    string QuestionnaireId = this.Request.QueryString["id"];
                    this.hiddenId.Value = QuestionnaireId;
                    V_TrQuestionnaire tq = ts.GetAllQuestionnaireInfoById(new Guid(QuestionnaireId));
                    if (tq != null)
                    {
                        this.txtName.Text = tq.QuestionnaireName;
                        this.txtBm.Text = tq.DeptName;
                        this.txtSTime.Text = tq.QStartTime.ToString();
                        this.txtETime.Text = tq.QEndTime.ToString();

                        IList<DatDepartment> listDept = ts.GetAllDepartment();
                        if (listDept != null && listDept.Count > 0)
                        {
                            this.drpDept.DataSource = listDept;
                            this.drpDept.DataTextField = "DeptName";
                            this.drpDept.DataValueField = "DeptID";
                            this.drpDept.DataBind();
                        }
                    }
                    //IList<V_TrSendQuestion> ListSq = ts.GetAllSendQuestionInfo(new Guid(QuestionnaireId));
                    //if (ListSq != null)
                    //{
                    //    for (int i = 0; i < ListSq.Count; i++)
                    //    { 
                    //        this.lstBoxResult.Items.Add(new ListItem(ListSq[i].UName,ListSq[i].UserID.ToString()));
                    //    }
                    //}
                }
            }
            else
            {
                string[] Ids = strJson.Trim(',').Split(',');
                GetUserId(Ids);
            }
        }
        public string GetUserNameByDeptId(Guid DeptId)
        {
            strList = ts.GetUserNameByDeptId(DeptId);
            return Globals.ObjectToJSON(strList);
        }
        public void GetUserId(string[] ids)
        {
            string s ="";
            if (ids != null)
            {
                for (int i = 0; i < ids.Length; i++)
                {
                    System.Guid guid = new Guid(ids[i]);
                    if (i < ids.Length - 1)
                    {
                        s += GetUserNameByDeptId(guid) + ",";
                        continue;
                    }
                    s += GetUserNameByDeptId(guid);
                }
                Response.Write(s);
                Response.End();
            }
        }

        /// <summary>
        /// 部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drpDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lstBoxSubject.Items.Clear();
            IList<V_TrSendQuestionnaireUser> listEmployee = null;
            if (this.drpDept.SelectedValue == "11")
            {
                listEmployee = ts.GetAllEmployeeInfo("11");
                this.lstBoxSubject.Items.Add(new ListItem("集团总部所有人", "11"));
            }
            else
            {
                listEmployee = ts.GetAllEmployeeInfo(this.drpDept.SelectedItem.Value);
                this.lstBoxSubject.Items.Add(new ListItem("部门所有人", "22"));
            }
            if (listEmployee != null && listEmployee.Count > 0)
            {
                for (int i = 0; i < listEmployee.Count; i++)
                {
                    this.lstBoxSubject.Items.Add(new ListItem(listEmployee[i].Name, listEmployee[i].UserID.ToString()));
                }
            }
            else
            {
                this.lstBoxSubject.Items.Clear();
            }
            this.hiddenDId.Value = this.drpDept.SelectedValue;
        }
    }
}